// pages/select_service/select_service.js
//来源 https://blog.csdn.net/m0_52457734/article/details/120083875
var api = require('../../api/api');
const { unix } = require('../../utils/dayjs.min');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navs:[],
    shop_id:'',
    curindex:0,
    lists:[],
    page:1,
    lastPage:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      const page=options.page;
      this.data.page_start=page;
      if(options.shop_id){
        this.setData({
          shop_id:options.shop_id
        })
      }
      this.getServiceCategory();

  },
  toServiceDetail(e){
    let index = e.currentTarget.dataset.index;
    let id = this.data.lists[index].id;
    wx.navigateTo({
      url: '/pages/service_details/service_details?id='+id,
      fail:function(res){
        console.log(res)
      }
    })
  },
  getServiceCategory(){
    api.get('/common/serviceCategory',{
      shop_id:this.data.shop_id
    }).then(res=>{
        this.setData({
          navs:res.items,
          cid:res.items ? res.items[0].id: ''
        })
        this.getServices();
    })
  },
  getServices(){

    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/common/services',{
      cid:this.data.cid,
      shop_id:this.data.shop_id
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  click_back(){
    wx.navigateBack({
      delta: 1,
    })
  },
  click_nav(e){
    const index=e.currentTarget.dataset.index;
  
    let that = this;
    console.log(that.data.navs)
    this.setData({
      curindex:index,
      cid:that.data.navs[index].id,
      lists:[],
      page:1,
      lastPage:0
    })
    this.getServices();
  },
  click_list_item(e){
      const page_start=this.data.page_start;
      const index=e.currentTarget.dataset.index;
      let service = this.data.lists[index];
      wx.setStorageSync('selectService', service)
      if(page_start=='submit'){
          var pages = getCurrentPages(); // 获取页面栈
          var prevPage = pages[pages.length - 2]; // 上一个页面
          prevPage.setData({
            selectedService: service,
            serviceTxt:service.title+" | ￥"+service.price
          });
          wx.navigateBack({
            delta: 1,
          })
      }else{
        wx.navigateTo({
            url: '../submit_order/submit_order?price='+service.price+'&tit='+service.title+'&sid='+service.id+"&shop_id="+this.data.shop_id,
          })
      }
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})